Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Imports Phoenix.DataAccess
Imports Phoenix.Events

Namespace Forms.Area

    Public Class ProductArea : Inherits GridAreaBase

#Region " StatusFormatter "
        Public Class StatusFormatter
            Implements IFormatProvider, ICustomFormatter

            Public Function GetFormat(ByVal type As System.Type) As Object Implements IFormatProvider.GetFormat
                Return Me
            End Function

            Public Function Format(ByVal formatString As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
                Dim p As DataSetData.Product = AppDataLayer.Products.FindByProductID(CInt(arg))

                If Not p Is Nothing Then
                    If p.Stock - p.CriticalStock <= 0 Then
                        Return Res.CriticalStockTitle
                    Else
                        Return Res.NormalStockTitle
                    End If
                End If
            End Function

        End Class
#End Region

        Public Event ProductSelectionChanged(ByVal sender As Object, ByVal args As EventArgs)

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            ProductStatusColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom
            ProductStatusColumn.DisplayFormat.Format = New StatusFormatter

            GridView = ProductView
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents GridControlProducts As DevExpress.XtraGrid.GridControl
        Friend WithEvents ProductView As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents ProductLabel As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ProductType As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ProductFamily As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ProductIconColumn As DevExpress.XtraGrid.Columns.GridColumn
        Friend WithEvents ProductStatusColumn As DevExpress.XtraGrid.Columns.GridColumn
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.GridControlProducts = New DevExpress.XtraGrid.GridControl
            Me.ProductView = New DevExpress.XtraGrid.Views.Grid.GridView
            Me.ProductLabel = New DevExpress.XtraGrid.Columns.GridColumn
            Me.ProductType = New DevExpress.XtraGrid.Columns.GridColumn
            Me.ProductFamily = New DevExpress.XtraGrid.Columns.GridColumn
            Me.ProductIconColumn = New DevExpress.XtraGrid.Columns.GridColumn
            Me.ProductStatusColumn = New DevExpress.XtraGrid.Columns.GridColumn
            CType(Me.GridControlProducts, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ProductView, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'GridControlProducts
            '
            Me.GridControlProducts.Dock = System.Windows.Forms.DockStyle.Fill
            '
            'GridControlProducts.EmbeddedNavigator
            '
            Me.GridControlProducts.EmbeddedNavigator.Name = ""
            Me.GridControlProducts.Location = New System.Drawing.Point(0, 0)
            Me.GridControlProducts.MainView = Me.ProductView
            Me.GridControlProducts.Name = "GridControlProducts"
            Me.GridControlProducts.Size = New System.Drawing.Size(248, 376)
            Me.GridControlProducts.Styles.AddReplace("FocusedCell", New DevExpress.Utils.ViewStyleEx("FocusedCell", "Grid", New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte)), CType((((((DevExpress.Utils.StyleOptions.StyleEnabled Or DevExpress.Utils.StyleOptions.UseBackColor) _
                                Or DevExpress.Utils.StyleOptions.UseDrawFocusRect) _
                                Or DevExpress.Utils.StyleOptions.UseFont) _
                                Or DevExpress.Utils.StyleOptions.UseForeColor) _
                                Or DevExpress.Utils.StyleOptions.UseImage), DevExpress.Utils.StyleOptions), System.Drawing.SystemColors.Highlight, System.Drawing.SystemColors.HighlightText, System.Drawing.SystemColors.InactiveCaption, System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
            Me.GridControlProducts.TabIndex = 1
            Me.GridControlProducts.Text = "Products"
            '
            'ProductView
            '
            Me.ProductView.Columns.AddRange(New DevExpress.XtraGrid.Columns.GridColumn() {Me.ProductLabel, Me.ProductType, Me.ProductFamily, Me.ProductIconColumn, Me.ProductStatusColumn})
            Me.ProductView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None
            Me.ProductView.GridControl = Me.GridControlProducts
            Me.ProductView.GroupPanelText = ""
            Me.ProductView.Name = "ProductView"
            Me.ProductView.OptionsBehavior.AllowIncrementalSearch = True
            Me.ProductView.OptionsBehavior.ShowEditorOnMouseUp = True
            Me.ProductView.OptionsSelection.InvertSelection = True
            Me.ProductView.OptionsSelection.MultiSelect = True
            Me.ProductView.OptionsView.ShowIndicator = False
            Me.ProductView.OptionsView.ShowVertLines = False
            '
            'ProductLabel
            '
            Me.ProductLabel.Caption = "Product"
            Me.ProductLabel.FieldName = "Label"
            Me.ProductLabel.Name = "ProductLabel"
            Me.ProductLabel.Options = CType((((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.ProductLabel.VisibleIndex = 1
            Me.ProductLabel.Width = 55
            '
            'ProductType
            '
            Me.ProductType.Caption = "Type"
            Me.ProductType.FieldName = "Type"
            Me.ProductType.Name = "ProductType"
            Me.ProductType.Options = CType((((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.ProductType.SortIndex = 0
            Me.ProductType.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending
            Me.ProductType.VisibleIndex = 2
            Me.ProductType.Width = 55
            '
            'ProductFamily
            '
            Me.ProductFamily.Caption = "Family"
            Me.ProductFamily.FieldName = "Family"
            Me.ProductFamily.Name = "ProductFamily"
            Me.ProductFamily.Options = CType((((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.ProductFamily.SortIndex = 1
            Me.ProductFamily.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending
            Me.ProductFamily.VisibleIndex = 3
            Me.ProductFamily.Width = 60
            '
            'ProductIconColumn
            '
            Me.ProductIconColumn.Name = "ProductIconColumn"
            Me.ProductIconColumn.Options = CType(((DevExpress.XtraGrid.Columns.ColumnOptions.ReadOnly Or DevExpress.XtraGrid.Columns.ColumnOptions.FixedWidth) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            Me.ProductIconColumn.VisibleIndex = 0
            Me.ProductIconColumn.Width = 20
            '
            'ProductStatusColumn
            '
            Me.ProductStatusColumn.Caption = "Status"
            Me.ProductStatusColumn.FieldName = "ProductID"
            Me.ProductStatusColumn.Name = "ProductStatusColumn"
            Me.ProductStatusColumn.Options = CType(((((((((DevExpress.XtraGrid.Columns.ColumnOptions.CanFiltered Or DevExpress.XtraGrid.Columns.ColumnOptions.CanMoved) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanGrouped) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanResized) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanSorted) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ReadOnly) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.CanFocused) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.ShowInCustomizationForm) _
                        Or DevExpress.XtraGrid.Columns.ColumnOptions.NonEditable), DevExpress.XtraGrid.Columns.ColumnOptions)
            '
            'ProductArea
            '
            Me.Controls.Add(Me.GridControlProducts)
            Me.Name = "ProductArea"
            Me.Size = New System.Drawing.Size(248, 376)
            CType(Me.GridControlProducts, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ProductView, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Event Handlers "
        Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            If DesignMode Then Return

            Me.GridControlProducts.DataSource = New DataView(AppDataLayer.Products)
        End Sub

        Private Sub ProductView_SelectionChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.SelectionChangedEventArgs) Handles ProductView.SelectionChanged
            RaiseEvent ProductSelectionChanged(Me, Nothing)
        End Sub

        Private Sub ProductView_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles ProductView.FocusedRowChanged
            Dim rows() As Integer = ProductView.GetSelectedRows
            If Not rows Is Nothing Then
                For Each row As Integer In rows
                    ProductView.UnselectRow(row)
                Next
            End If

            ProductView.SelectRow(e.FocusedRowHandle)
        End Sub

        Private mHashColor As Hashtable = New Hashtable
        Private Sub ProductView_CustomDrawCell(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles ProductView.CustomDrawCell
            Dim p As DataSetData.Product = ProductView.GetDataRow(e.RowHandle)

            If e.Column Is ProductIconColumn Then
                DrawProductIconColumn(p, e)
            End If

            If (p.Stock - p.CriticalStock <= 0) And AppConfig.IndicateCriticalStock Then
                If Not mHashColor.ContainsKey(e.Style) Then
                    mHashColor(e.Style) = e.Style.ForeColor
                End If
                e.Style.ForeColor = AppConfig.CriticalStockColor
            Else
                If mHashColor.ContainsKey(e.Style) Then
                    e.Style.ForeColor = CType(mHashColor(e.Style), Color)
                End If
            End If
        End Sub

        Private Sub DrawProductIconColumn(ByVal p As DataSetData.Product, ByVal e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs)
            Dim image As Image

            If AppConfig.IndicateCriticalStock Then
                If Not p Is Nothing Then
                    If p.Stock - p.CriticalStock <= 0 Then
                        image = AppSharedComponents.ListImages.Images(5)
                    End If
                End If
            End If

            If image Is Nothing Then image = AppSharedComponents.ListImages.Images(5)

            Dim r As Rectangle = e.Bounds
            e.Graphics.DrawImage(image, r.Left, r.Top)
            e.Handled = True
        End Sub

#End Region

#Region " Area Behaviour "

        Public Overrides ReadOnly Property IconColumn() As DevExpress.XtraGrid.Columns.GridColumn
            Get
                Return ProductIconColumn
            End Get
        End Property


        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If
            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetData.Product = Helpers.ProductHelper.Copy(CType(obj, DataAccess.DataSetData.Product))
                'AppDataLayer.Products.AddProduct(item)
                Helpers.ProductHelper.UpdateProduct(item)
            Next
        End Sub

        Public Overrides Sub Delete(ByVal arr As IList)
            If arr Is Nothing Then Return

            Helpers.ProductHelper.DeleteProducts(arr)
        End Sub

        Public Overrides Function CanCopy() As Boolean
            Return Security.CanCreateProduct
        End Function

        Public Overrides Function CanDelete() As Boolean
            Return Security.CanDeleteProduct
        End Function

#End Region

#Region " Others "
        Public Overrides Sub UpdateConfiguration()
            MyBase.UpdateConfiguration()
            ProductView.RowHeight = AppSharedComponents.ListImages.ImageSize.Height + AppIconColumnPadding / 2
            GridView.Invalidate()
        End Sub
#End Region

    End Class

End Namespace